﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace RoAssistant.Client
{
    /// <summary>
    /// 将list生成为tci文件
    /// </summary>
    class ChangeListToTci
    {
        /// <summary>
        /// XMLFormat程序
        /// </summary>
        private class XmlFormat
        {
            /// <summary>
            /// XDocument形式的临时xml文件
            /// </summary>
            private XDocument _temptciDocument;


            /// <summary>
            /// xml文件的根节点
            /// </summary>
            private XElement _rootElement;

            /// <summary>
            /// 创建tci文件
            /// </summary>
            /// <param name="elementInfos">元素信息List</param>
            public void CreateTempTci(List<ClientElement> elementInfos)
            {
                _temptciDocument = new XDocument();
                string name = elementInfos[0].ControlId;
                XNamespace tcicore = "http://tempuri.org/RoTciClient.xsd";
                _rootElement = new XElement(tcicore + "ElementInfo", new XAttribute(XNamespace.Xmlns + "tcicore", "http://tempuri.org/RoTciClient.xsd"));
                _temptciDocument.Add(_rootElement);
                XElement resElement = new XElement(tcicore + "Resources");
                _rootElement.Add(resElement);
                foreach (ClientElement sigElementInfo in elementInfos)
                {
                    XElement sigXElement = CreateControlXmlNode(sigElementInfo, tcicore);
                    resElement.Add(sigXElement);
                }
                _temptciDocument.Save("C:\\Temp\\" + name + ".tci");
            }


            /// <summary>
            /// 将所有的list内信息拷贝进入tci文件
            /// </summary>
            /// <param name="elementInfo">单个elementinfo</param>
            /// <param name="tcicore">命名空间</param>
            /// <returns></returns>
            private XElement CreateControlXmlNode(ClientElement elementInfo, XNamespace tcicore)
            {
                XElement retXElement = null;
                string programmaticName = elementInfo.ControlType.ProgrammaticName;

                switch (programmaticName)
                {
                    case "ControlType.Window":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Window");
                        break;
                    case "ControlType.Button":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Button");
                        break;
                    case "ControlType.Calendar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Calendar");
                        break;
                    case "ControlType.CheckBox":
                        retXElement = GetNewXElement(tcicore, elementInfo, "CheckBox");
                        break;
                    case "ControlType.ComboBox":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ComboBox");
                        break;
                    case "ControlType.Custom":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Custom");
                        break;
                    case "ControlType.DataGrid":
                        retXElement = GetNewXElement(tcicore, elementInfo, "DataGrid");
                        break;
                    case "ControlType.DataItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "DataItem");
                        break;
                    case "ControlType.Document":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Document");
                        break;
                    case "ControlType.Edit":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Edit");
                        break;
                    case "ControlType.Group":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Group");
                        break;
                    case "ControlType.Header":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Header");
                        break;
                    case "ControlType.HeaderItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "HeaderItem");
                        break;
                    case "ControlType.Hyperlink":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Hyperlink");
                        break;
                    case "ControlType.Image":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Image");
                        break;
                    case "ControlType.List":
                        retXElement = GetNewXElement(tcicore, elementInfo, "List");
                        break;
                    case "ControlType.ListItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ListItem");
                        break;
                    case "ControlType.Menu":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Menu");
                        break;
                    case "ControlType.MenuBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "MenuBar");
                        break;
                    case "ControlType.MenuItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "MenuItem");
                        break;
                    case "ControlType.Pane":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Pane");
                        break;
                    case "ControlType.ProgressBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ProgressBar");
                        break;
                    case "ControlType.RadioButton":
                        retXElement = GetNewXElement(tcicore, elementInfo, "RadioButton");
                        break;
                    case "ControlType.ScrollBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ScrollBar");
                        break;
                    case "ControlType.Separator":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Separator");
                        break;
                    case "ControlType.Slider":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Slider");
                        break;
                    case "ControlType.Spinner":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Spinner");
                        break;
                    case "ControlType.SplitButton":
                        retXElement = GetNewXElement(tcicore, elementInfo, "SplitButton");
                        break;
                    case "ControlType.StatusBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "StatusBar");
                        break;
                    case "ControlType.Tab":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Tab");
                        break;
                    case "ControlType.TabItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "TabItem");
                        break;
                    case "ControlType.Table":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Table");
                        break;
                    case "ControlType.Text":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Text");
                        break;
                    case "ControlType.Thumb":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Thumb");
                        break;
                    case "ControlType.TitleBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "TitleBar");
                        break;
                    case "ControlType.ToolBar":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ToolBar");
                        break;
                    case "ControlType.ToolTip":
                        retXElement = GetNewXElement(tcicore, elementInfo, "ToolTip");
                        break;
                    case "ControlType.Tree":
                        retXElement = GetNewXElement(tcicore, elementInfo, "Tree");
                        break;
                    case "ControlType.TreeItem":
                        retXElement = GetNewXElement(tcicore, elementInfo, "TreeItem");
                        break;
                }


                return retXElement;
            }


            /// <summary>
            /// 获取新的元素
            /// </summary>
            /// <param name="tcicore">命名空间</param>
            /// <param name="elementInfo">元素信息</param>
            /// <param name="controltype">元素类型</param>
            /// <returns></returns>
            private XElement GetNewXElement(XNamespace tcicore, ClientElement elementInfo, string controltype)
            {
                return
                    new XElement(tcicore + controltype,
                        new XAttribute(tcicore + "ID", elementInfo.ControlId),
                        new XElement(tcicore + "ReferenceContainer",
                            new XAttribute(tcicore + "Scope", "Children"), elementInfo.ReferenceContainer),
                        new XElement(tcicore + "SearchProperties",
                            new XElement(tcicore + "SearchProperty",
                                new XAttribute(tcicore + "Name", "AutomationId"), elementInfo.ElementAutomationId),
                            new XElement(tcicore + "SearchProperty",
                                new XAttribute(tcicore + "Name", "Name"), elementInfo.ElementName),
                            new XElement(tcicore + "SearchProperty",
                                new XAttribute(tcicore + "Name", "ClassName"), elementInfo.ElementClassName)));
            }
        }


        /// <summary>
        /// 将List列表更改为TCI文件
        /// </summary>
        /// <param name="elementInfos">元素信息list</param>
        public void ChangeListToTciFile(List<ClientElement> elementInfos)
        {
            XmlFormat xmlFormat = new XmlFormat();
            xmlFormat.CreateTempTci(elementInfos);
        }
    }
}
